<?php

namespace app\index\controller;

use think\Request;
use think\Db;
use think\Cache;

class Address extends Common 
{
	private $_prefix = 'address_area_';

	public function index() 
	{
		return $this->fetch();
	}

	/**
	 * ajax返回省市区信息
	 * @return array
	 */
	public function getArea()
	{	
		if (Request::instance()->isPost()) {
			$id  = input('post.id');
			$res = $this->_getAreaById($id);
			return $this->success($res);
		} else {
			return $this->error('非法访问');
		}
	}

	/**
	 * ajax返回全部省市区信息
	 * @return array
	 */
	public function getAllArea() {
		if (Request::instance()->isPost()) {
			$ids = input('post.id/a');
			sort($ids);
			$res = array();
			foreach ($ids as $id) {
				$res[] = $this->_getAreaById($id);
			}
			return $this->success($res);
		} else {
			return $this->error('非法访问');
		}	
	}

	/**
	 * 根据id获取地区信息
	 * @param  int   $id 地区id
	 * @return array     下级地区信息
	 */
	private function _getAreaById($id) {
		$name = $this->_prefix . $id;
		$res = Cache::get($name);
		if ($res == false) {
			$res = DB::name('area')->field('id, areaname')->where('parentid', $id)->order('sort')->select();
		}
		return $res;
	}

	/**
	 * ajax新增地址
	 * @return mixed
	 */
	public function add()
    {
	    if (Request::instance()->isPost()) {
	    	$province 		= input('post.province');
			$city 			= input('post.city');
			$county 		= input('post.county');
			$address 		= input('post.address');
			$consignee      = input('post.consignee');
			$mobile 		= input('post.mobile');
			$default        = input('post.default');


			if (empty($province) || empty($city) || empty($address) || empty($mobile) || empty($consignee)) {
				return $this->error('参数错误！');
			}

			$data['uid'] 	    = UID;
			$data['province']   = $province;
			$data['city'] 	    = $city;
			$data['county']     = $county;
			$data['address']    = $address;
			$data['mobile']     = $mobile;
			$data['consignee']  = $consignee;
			$data['is_default'] = $default ? 1 : 0;

			$res = DB::name('address')->insert($data);
			if ($res) {
				return $this->success('地址添加成功');
			} else {
				return $this->error('地址添加失败');
			}
	    } else {
	    	return $this->error('非法访问');
	    }
    }

    /**
     * ajax修改地址
     * @return mixed
     */
	public function edit()
    {	
	    if (Request::instance()->isPost()) {
	    	$id        = input('post.id');
	    	$province  = input('post.province');
			$city 	   = input('post.city');
			$county    = input('post.county');
			$address   = input('post.address');
			$consignee = input('post.consignee');
			$mobile    = input('post.mobile');
			$default   = input('post.default');

			$data['province']   = $province;
			$data['city'] 	    = $city;
			$data['county']     = $county;
			$data['address']    = $address;
			$data['mobile']     = $mobile;
			$data['consignee']  = $consignee;
			$data['is_default'] = $default ? 1 : 0;

			$data = array_filter($data);

			$res = DB::name('address')->where('id', $id)->update($data);
			if ($res) {
				return $this->success('地址修改成功');
			} else {
				return $this->error('地址修改失败');
			}
	    } else {
	    	return $this->error('非法访问');
	    }
    }

    /**
     * ajax设置默认地址
     * @return mixed
     */
    public function setDefault() {
    	if (Request::instance()->isPost()) {
    		$id = input('post.id');
    		$res1 = DB::name('address')->where('uid', UID)->where('is_default', '1')->setDec('is_default');
    		$res2 = DB::name('address')->where('id', $id)->setInc('is_default');
    		if ($res2) {
				return $this->success($id.'te');
			} else {
				return $this->error('地址修改失败');
			}
    	} else {
	    	return $this->error('非法访问');
    	}
    }

    /**
     * ajax删除地址
     * @return mixed
     */
    public function delete() {
    	if (Request::instance()->isPost()) {
    		$id = input('post.id');
    		$res = DB::name('address')->where('id', $id)->delete();
    		if ($res) {
				return $this->success('地址删除成功');
			} else {
				return $this->error('地址修改失败');
			}
    	} else {
	    	return $this->error('非法访问');
    	}
    }
}
